package com.threeonemeeting.util;

import java.io.File;

import android.content.Context;
import android.util.Log;

import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.task.BaseAsyncTask;
import com.snda.lib.ui.MessageBox;
import com.snda.lib.util.MiscHelper;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.model.DownloadInfo;
import com.threeonemeeting.model.DownloadInfoList;
import com.threeonemeeting.model.MeetingDataDocInfo;
import com.threeonemeeting.task.DownloadDocumentFileTask;

public class TaskUtil {

	/**
	 * @desc 根据MeetingDataDocInfo新建一个下载任务
	 * @param mMeetingDataDocInfo
	 * @param appContext
	 * @param cb
	 */
	public static void addDownloadTask(MeetingDataDocInfo mMeetingDataDocInfo,
			ICallBack cb) {
		if (mMeetingDataDocInfo == null)
			return;

		Context appContext = DataCenter.getInstance().getApplicationContext();

		String strDownloadUrl = mMeetingDataDocInfo.strFileUrl;

		DownloadInfoList listDownload = DataCenter.getInstance().listDownload;
		DownloadInfo dInfo = null;
		if (listDownload != null) {
			dInfo = listDownload.getDownloadInfoByUrl(Const.URL.BASE_SERVER_URL
					+ strDownloadUrl);
			if (dInfo != null
					&& dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_RUNNING) {
				MessageBox.showToast(appContext,
						mMeetingDataDocInfo.getShowName() + "已经在下载队列中");
				return;
			}
		}

		Log.d(Const.Tag, "Download URL:");
		Log.d(Const.Tag, strDownloadUrl);

		DownloadDocumentFileTask downloadTask = new DownloadDocumentFileTask(
				appContext, cb);
		downloadTask.setUrl(Const.URL.BASE_SERVER_URL + strDownloadUrl);
		downloadTask.setSavePath(AppSettings.getFilePath(appContext));
		downloadTask.setAppKey(String.valueOf(mMeetingDataDocInfo.nDocId));
		downloadTask.setFileName(mMeetingDataDocInfo.strFileName);
		downloadTask.setFileTitle(mMeetingDataDocInfo.strFileTitle);

		int nRet = -1;

		if (dInfo != null) {
			if (dInfo.nCookie != 0) {
				nRet = TaskManager.continueTask(dInfo.nCookie, downloadTask,
						null, false);
			} else {
				listDownload.deleteDownloadInfo(dInfo.strAppKey);
				nRet = TaskManager.addTask(downloadTask, null, false);
			}
		} else {
			nRet = TaskManager.addTask(downloadTask, null, false);
		}

		if (nRet == 0) {
			MessageBox.showToast(appContext, mMeetingDataDocInfo.getShowName()
					+ "已经在下载队列中");
		} else {
			if (dInfo != null) {
				dInfo.nStatus = DownloadInfo.DOWNLOAD_STATUS_RUNNING;
			}
		}
	}

	/**
	 * @desc 根据已有的DownloadInfo创建一个新的下载任务
	 * @param dInfo
	 * @param cb
	 */
	public static void addDownloadTask(DownloadInfo dInfo, ICallBack cb) {
		if (dInfo == null)
			return;

		if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_RUNNING) {
			MessageBox.showToast(DataCenter.getInstance()
					.getApplicationContext(), dInfo.getShowName() + "已经在下载队列中");
			return;
		}

		DownloadInfoList listDownload = DataCenter.getInstance().listDownload;
		listDownload.deleteDownloadInfo(dInfo.strAppKey);

		DownloadDocumentFileTask downloadTask = new DownloadDocumentFileTask(
				DataCenter.getInstance().getApplicationContext(), cb);
		downloadTask.setUrl(dInfo.strFileUrl);
		downloadTask.setSavePath(dInfo.strLocalPath);
		downloadTask.setContinue(false);
		downloadTask.setAppKey(dInfo.strAppKey);
		TaskManager.addTask(downloadTask, null, false);

		dInfo.nStatus = DownloadInfo.DOWNLOAD_STATUS_RUNNING;
	}

	/**
	 * @desc 根据已有的DownloadInfo创建一个新的下载任务
	 * @param dInfo
	 * @param cb
	 */
	public static void reDownloadTask(DownloadInfo dInfo, ICallBack cb) {
		addDownloadTask(dInfo, cb);
	}

	/**
	 * @desc 对指定的任务进行续传
	 * @param dInfo
	 */
	public static void continueDownloadTask(DownloadInfo dInfo, ICallBack cb) {
		if (dInfo == null)
			return;

		String strShowText = dInfo.strFileTitle;
		if (MiscHelper.isEmpty(strShowText) == true)
			strShowText = dInfo.strFileName;

		if (dInfo.nStatus == DownloadInfo.DOWNLOAD_STATUS_RUNNING) {
			MessageBox.showToast(DataCenter.getInstance()
					.getApplicationContext(), dInfo.getShowName() + "已经在下载队列中");
			return;
		}

		String strDownloadUrl = dInfo.strFileUrl;

		Log.d(Const.Tag, "Download URL:");
		Log.d(Const.Tag, strDownloadUrl);

		DownloadDocumentFileTask downloadTask = new DownloadDocumentFileTask(
				DataCenter.getInstance().getApplicationContext(), cb);
		downloadTask.setUrl(strDownloadUrl);

		File f = new File(dInfo.strLocalPath);
		String strSavePath = f.getParent();
		downloadTask.setSavePath(strSavePath);
		downloadTask.setAppKey(dInfo.strAppKey);

		if (dInfo.nCookie != 0) {
			TaskManager.continueTask(dInfo.nCookie, downloadTask, null, false);
		} else {
			// listDownload.deleteDownloadInfo(dInfo.strAppKey);
			TaskManager.addTask(downloadTask, null, false);
		}

		dInfo.nStatus = DownloadInfo.DOWNLOAD_STATUS_RUNNING;
	}

	public static void cancelTask(DownloadInfo dInfo) {
		if (dInfo != null) {

			BaseAsyncTask httpAsync = (BaseAsyncTask) TaskManager.getInstance()
					.getTask(dInfo.nCookie);
			if (httpAsync != null) {

				// 取消下载
				httpAsync.cancelTask();
				dInfo.nStatus = DownloadInfo.DOWNLOAD_STATUS_CANCEL;
			}

			DataCenter.getInstance().getDB().addDownloadInfo(dInfo);
		}
	}

	/**
	 * @desc 将所有的任务切换为 WiFi下载
	 */
	public static void swithTaskToWiFi() {

		DownloadInfoList listDownload = DataCenter.getInstance().listDownload;
		if (listDownload == null)
			return;

		for (int i = 0; i < listDownload.getSize(); i++) {
			DownloadInfo dInfo = listDownload.getAt(i);
			if (dInfo == null)
				continue;

			TaskUtil.cancelTask(dInfo);
			dInfo.bWiFiDownload = true;
			dInfo.nStatus = DownloadInfo.DOWNLOAD_STATUS_WAITINGWIFI;
		}

	}
}
